package com.ttl.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttl.web.entity.PageParam;
import com.ttl.web.entity.User;
import com.ttl.web.service.UserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import com.ttl.web.utils.ResultVo;

import java.util.Optional;

@Api(tags = "用户端控制器")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ResultVo register(@RequestBody User user) {
        //判断用户名是否存在
        if (userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername())) != null) {
            return ResultVo.error("用户名已存在");
        }
        //判断手机号是否存在
        if (userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, user.getPhone())) != null) {
            return ResultVo.error("手机号已存在");
        }
        //密码hashMD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //保存用户
        if (userService.save(user)) {
            return ResultVo.success("注册成功");
        }else {
            return ResultVo.error("注册失败");
        }
    }


    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResultVo login(@RequestBody User user) {
        //判断用户名是否存在
        User u = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
        if (u == null) {
            return ResultVo.error("用户不存在");
        }
        //密码hashMD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //判断密码是否正确
        if (!u.getPassword().equals(user.getPassword())) {
            return ResultVo.error("密码错误");
        }
        //判断是否被停用
        if (u.getStatus().equals("1")) {
            return ResultVo.error("账号已被停用,请联系管理员");
        }
        //返回结果
        return ResultVo.success("登录成功",u);
    }

    /**
     * 新增
     * @param user
     */
    @PostMapping
    public ResultVo add(@RequestBody User user) {
        if (userService.save(user)) {
            return ResultVo.success("新增成功");
        }else {
            return ResultVo.error("新增失败");
        }
    }

    /**
     * 编辑
     */
    @PutMapping
    public ResultVo edit(@RequestBody User user) {
        if (userService.updateById(user)) {
            return ResultVo.success("编辑成功");
        }else {
            return ResultVo.error("编辑失败");
        }
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public ResultVo delete(@PathVariable Long userId) {
        if (userService.removeById(userId)) {
            return ResultVo.success("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }

    /**
     * 查询列表
     * @param parm
     * @return
     */
    @GetMapping
    public ResultVo getList(PageParam parm) {
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(parm.getName())) {
            queryWrapper.like(User::getNickName, parm.getName());
        }
        // 创建分页对象
        Page<User> page = new Page<>(Optional.ofNullable(parm.getCurrentPage()).orElse(1), parm.getPageSize());
        // 执行分页查询
        IPage<User> pageList = userService.page(page, queryWrapper);
        // 返回结果
        return ResultVo.success(pageList);
    }

}
